package com.eb.controller;


import com.eb.service.GoodsServicelmp;
import com.eb.service.MiaoshaService;
import com.eb.pojo.Goods;
import com.eb.pojo.MiaoshaGoods;
import com.eb.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.PageUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("miaosha")
@CrossOrigin
public class MiaoshaController {
    @Autowired
    private MiaoshaService miaoshaService;
    @Autowired
    private GoodsServicelmp goodsClicent;
    @GetMapping
    public List<MiaoshaGoods> list(){
        return  miaoshaService.list();
    }

    @GetMapping("/{id}")
    public Goods goodsid(@PathVariable("id")int id){
        return goodsClicent.findBygoods(id);
    }
    @GetMapping("/GoodsList")
    public List<Goods> goods(){
        return goodsClicent.list();
    }
    @GetMapping("miaoshaGoods")
    public PageUtil<Map<String,Object>> miaoshaGoods(@RequestParam(value = "goodsName",defaultValue = "",required = false)String goodsName,
                                                     @RequestParam(value = "start",defaultValue = "1970-01-01",required = false)String start,
                                                     @RequestParam(value = "end",defaultValue = "2099-01-01",required = false)String end,
                                                     @RequestParam(value = "page",defaultValue = "1",required = false)int page,
                                                     @RequestParam(value = "size",defaultValue = "10",required = false)int size){
        Page<Map<String,Object>> miaoshaGoods=miaoshaService.miaoshaGoods(goodsName,start,end,page,size);
        return new PageUtil<Map<String,Object>>(page,miaoshaGoods.getTotalElements(),miaoshaGoods.getContent(),size);
    }
    @PostMapping
    public Result MiaoshaAdd(@RequestBody MiaoshaGoods miaoshaGoods){
        try {
            miaoshaService.MiaoshaAdd(miaoshaGoods);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id")int id){
        try {
            miaoshaService.del(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
    @PutMapping
    public Result UpdateMiaosha(@RequestBody MiaoshaGoods miaoshaGoods){
        try {
            miaoshaService.UpdateMiaosha(miaoshaGoods);
            return new Result(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }
    @GetMapping("/findByid/{id}")
    public Map<String,Object> findByid(@PathVariable("id")int id){
        MiaoshaGoods miaoshagoods= miaoshaService.findByid(id);
        Goods goods=goodsClicent.findBygoods(Math.toIntExact(miaoshagoods.getGoodsId()));
        Map<String, Object> map = new HashMap<>();
        map.put("goodsName",goods.getGoodsName());
        map.put("goodsImg",goods.getGoodsImg());
        map.put("goodsPrice",goods.getGoodsPrice());
        map.put("goodsStock",goods.getGoodsStock());
        map.put("id",miaoshagoods.getId());
        map.put("goodsId",miaoshagoods.getGoodsId());
        map.put("miaoshaPrice",miaoshagoods.getMiaoshaPrice());
        map.put("stockCount",miaoshagoods.getStockCount());
        map.put("startDate",miaoshagoods.getStartDate());
        map.put("endDate",miaoshagoods.getEndDate());
        return map;
    }
    @GetMapping("/goods/goodslist")
    public List<Map<String,Object>> goodslist(){
        List<Map<String,Object>> mapList=  new ArrayList<Map<String, Object>>();
        List<MiaoshaGoods> list = miaoshaService.list();
        for (MiaoshaGoods goods : list) {
            Goods bygoods = goodsClicent.findBygoods(Math.toIntExact(goods.getGoodsId()));
            Map<String, Object> map = new HashMap<>();
            map.put("goodsImg",bygoods.getGoodsImg());
            map.put("goodsPrice",bygoods.getGoodsPrice());
            map.put("goodsStock",bygoods.getGoodsStock());
            map.put("goodsName",bygoods.getGoodsName());
            map.put("id",goods.getId());
            map.put("goodsId",goods.getGoodsId());
            map.put("miaoshaPrice",goods.getMiaoshaPrice());
            map.put("stockCount",goods.getStockCount());
            map.put("startDate",goods.getStartDate());
            map.put("endDate",goods.getEndDate());
            mapList.add(map);
        }
        return mapList;
    }
}
